package com.edushare.common.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.edushare.common.mailer.ExistingUserInvite;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.UserDeptLink;
import com.edushare.common.vo.Users;

public class UserDeptLinkDAO extends CayenneBasedDAO {

	public UserDeptLinkDAO(ORMInterface ormInterface) {
		super(ormInterface);
	}

	public boolean isUserPresent(Departments dept, Users u) {
		List<UserDeptLink> udL = u.getDepartmentLinks();
		for (UserDeptLink ud : udL) {
			if (ud.getDepartmentLink() == dept)
				return true;
		}
		return false;
	}

	public List<Departments> getDeptByUserRole(Users u, int role){
		List<UserDeptLink> udL = u.getDepartmentLinks();
		List<Departments> deptList = new ArrayList<Departments>();
		for (UserDeptLink ud : udL) {
			if (ud.getRoleType() == role)
				deptList.add(ud.getDepartmentLink());
		}
		return deptList;
	}

	public UserDeptLink addUser(Departments dept, Users u, int roleType) {
		if (isUserPresent(dept, u))
			return null;
		UserDeptLink udl = new UserDeptLink();
		udl.setCreationTimestamp(new Date());
		udl.setDepartmentLink(dept);
		udl.setRoleType(roleType);
		udl.setUserLink(u);
		udl.setStatus(UserDeptLink.STATUS_INVITED);
		getDataContext().registerNewObject(udl);
		getDataContext().commitChanges();
		return udl;
	}

	public String addUserFB(Departments dept, Users u, int roleType, Users from) {
		if (isUserPresent(dept, u))
			return "User " + u.getCompleteUserName() + " already present in "
					+ dept.getDeptName();
		else {
			if (addUser(dept, u, roleType) != null) {
				if (u.getStatus() == Users.ACTIVE) {
					new OutgoingEmailsDAO(ormInterface)
							.sendMail(new ExistingUserInvite(u, from, dept)
									.send());
				}
				return "User " + u.getCompleteUserName() + " added to "
						+ dept.getDeptName();
			} else
				return "Unable to add user " + u.getCompleteUserName()
						+ " due to an unknown error";
		}
	}

	public List<Users> getUserByRole(Departments dept, int roleType){
		List<Users> userList = new ArrayList<Users>();
		List<UserDeptLink> udlList = dept.getUserLinks();
		for(UserDeptLink udl:udlList){
			if(udl.getRoleType() == roleType){
				userList.add(udl.getUserLink());
	}
		}
		return userList;
	}

	public UserDeptLink changeRole(Departments dept, Users u, int roleType) {
		List<UserDeptLink> udlL = u.getDepartmentLinks();
		for (UserDeptLink udl : udlL) {
			if (udl.getDepartmentLink() == dept) {
				udl.setRoleType(roleType);
				getDataContext().commitChanges();
				return udl;
			}
		}
		return null;
	}

	public void remove(Departments dept, Users u) {
		List<UserDeptLink> udlL = u.getDepartmentLinks();
		UserDeptLink deleteLink = null;
		for (UserDeptLink udl : udlL) {
			if (udl.getDepartmentLink() == dept) {
				deleteLink = udl;
			}
		}

		List<Courses> courseList = new CourseDeptLinkDAO(ormInterface)
				.getOwnedCourses(dept);
		for (Courses c : courseList) {
			CourseInstances cInst = new CourseInstancesDAO(ormInterface)
					.getActiveInst(c);
			new EnrolmentsDAO(ormInterface).removeUser(cInst, u);
		}

		if (deleteLink != null)
			getDataContext().deleteObject(deleteLink);
		getDataContext().commitChanges();
	}

	public List<UserDeptLink> getOutstandingInvites(Users user) {
		List<UserDeptLink> udlL = user.getDepartmentLinks();
		List<UserDeptLink> rtList = new ArrayList<UserDeptLink>();
		for (UserDeptLink udl : udlL) {
			if (udl.getStatus() == UserDeptLink.STATUS_INVITED)
				rtList.add(udl);
		}
		return rtList;
	}
}
